﻿using System;
using System.Xml;
using System.IO;

namespace personalplaner.core.errorhandling
{
	public class Logger
	{

        private static string FileName
        {
            get
            {
                string dir = AppDomain.CurrentDomain.BaseDirectory;

                var writePermission = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.Write, dir);
                if (!System.Security.SecurityManager.IsGranted(writePermission))
                {
                    dir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Personalplaner";
                    if (!Directory.Exists(dir))
                        Directory.CreateDirectory(dir);
                }

                return dir + "Error.xml";
            }
        }

		/// <summary>
		/// NOT YET IMPLEMENTED
		/// </summary>
		/// <param name="errortext"></param>
		public static void ToXML(string errortext)
		{
			ToXML("Error occured", "Undefined location", errortext);
		}

		/// <summary>
		/// NOT FINISHED IMPLEMENTATION
		/// </summary>
		/// <param name="caption"></param>
		/// <param name="location"></param>
		/// <param name="errorText"></param>
		public static void ToXML(string caption, string location, string errorText)
		{
			string date = DateTime.Now.ToString();

			XmlDocument document = new XmlDocument();

			XmlNode rootElement = null;
			if (File.Exists(FileName))
			{
				try
				{
					document.Load(FileName);
					rootElement = document.SelectSingleNode("./ErrorDocument");
				}
				catch { }
			}
			else
			{
				XmlDeclaration decl = document.CreateXmlDeclaration("1.0", "iso-8859-1", "");
				document.AppendChild(decl);

				rootElement = document.CreateElement("ErrorDocument");
				document.AppendChild(rootElement);
			}

			if (rootElement != null)
			{
				XmlNode mainElement = document.CreateElement("Error");
				rootElement.AppendChild(mainElement);

				XmlAttribute atr = document.CreateAttribute("Time");
				atr.InnerText = date;
				mainElement.Attributes.Append(atr);

				atr = document.CreateAttribute("Location");
				atr.InnerText = location;
				mainElement.Attributes.Append(atr);

				XmlNode errorElement = document.CreateElement("Errormesssage");
				errorElement.InnerText = errorText;
				mainElement.AppendChild(errorElement);

                try
                {
                    document.Save(FileName);
                }
                catch (Exception)
                {
                    try
                    {
                        string dir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Personalplaner";
                        if (!Directory.Exists(dir))
                            Directory.CreateDirectory(dir);

                        document.Save(dir + "\\Error.xml");
                    }
                    catch { }
                }
			}
		}
	}
}
